﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using System.Text.RegularExpressions;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class LanguagePage : WatiNBase
    {
        #region Constructors

        public LanguagePage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public LanguagePage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region SelectLists
        public SelectList LocaleSelectList
        {
            get { return ContentPane.Div(Find.ById(s => s.EndsWith("_ModuleContent"))).SelectLists[0]; }
        }
        public SelectList LanguageSelect
        {
            get { return IEInstance.Div(Find.ByClass("c_content")).SelectLists[0]; }
        }
        public SelectList FallbackLanguageSelect
        {
            get { return IEInstance.Div(Find.ByClass("c_content")).SelectLists[1]; ; }
        }
        public SelectList LanguagePackLocaleSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("LanguagePackWriter_cboLanguage")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("LanguagePackWriter_cboLanguage")));
            }
        }
        #endregion

        #region Links
        public Link AddLanguageDropDownArrow
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("_EditLanguage_ctl00_Arrow")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("_EditLanguage_ctl00_Arrow")));
            }
        }
        public Link AddNewLanguageLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Add New Language"))); }
        }
        public Link CreateLanguagePackLink
        {
            get { return IEInstance.Link(Find.ById(s => s.EndsWith("createLanguagePackLink"))); }
        }
        public Link CreateLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Create"));
                }
                return ContentPane.Link(Find.ByTitle("Create"));
            }
        }
        public Link DefaultLanguageDropDownLink
        {
            get { return ContentPane.Link(Find.ById(s => s.EndsWith("languageEnabler_ctl01_Arrow"))); }
        }
        public Link DeleteResourceFileLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Delete Resource File"));
                }
                return IEInstance.Link(Find.ByTitle("Delete Resource File"));
            }
        }
        public Link EditLanguageLink
        {
            get { return IEInstance.Link(Find.ByTitle("Edit Language")); }
        }
        public Link EnableLocalizedContentConfirmation
        {
            get
            {
                if(PopUpFrame!= null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Enable Localized Content"));
                }
                return EnableLocalizedContentPopUp.Link(Find.ByTitle("Enable Localized Content"));
            }
        }
        public Link EnableLocalizedContentLink
        {
            get { return LanguagesDiv.Link(Find.ById(s => s.EndsWith("languageEnabler_cmdEnableLocalizedContent"))); }
        }
        public Link FallbackLanguageDropDownArrow
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("_EditLanguage_ctl03_Arrow")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("_EditLanguage_ctl03_Arrow")));
            }
        }
        public Link InstallLanguagePackLink
        {
            get { return ContentPane.Link(Find.ById(s => s.EndsWith("installLanguagePackLink"))); }
        }
        public Link LanguageSettingsLink
        {
            get { return ContentPane.Link(Find.ByTitle("Language Settings")); }
        }
        public Link ReturnLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Return"));
                }
                return ContentPane.Link(Find.ByText("Return"));
            }
        }
        public Link SaveResourceFileLink
        {
            get { return ContentPane.Link(Find.ByTitle("Save Resource File")); }
        }
        public Link TimeZoneEditorLink
        {
            get { return IEInstance.Link(Find.ByTitle("Time Zone Editor")); }
        }
        public Link UpdateLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Update"));
                }
                return IEInstance.Link(Find.ByTitle("Update"));
            }
        }
        public Link VerifyLanguageResourceFilesLink
        {
            get { return ContentPane.Link(Find.ById(s => s.EndsWith("verifyLanguageResourcesLink"))); }
        }
        #endregion

        #region Tables
        public Table LanguageTable
        {
            get { return IEInstance.Table(Find.ById(new Regex(@"dnn_ctr\d*_languageEnabler_languagesGrid_ctl\d*"))); }
        }
        public Table ModuleCheckboxTable
        {
            get { return ContentPane.Table(Find.ById(s => s.EndsWith("LanguagePackWriter_lstItems"))); }
        }
        #endregion

        #region TextFields
        public TextField AdminStringTextField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextFields.Filter(Find.ById(s => s.EndsWith("_txtValue")))[1];
                }
                return IEInstance.TextFields.Filter(Find.ById(s => s.EndsWith("_txtValue")))[1];
            }
        }
        public TextField FileNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("LanguagePackWriter_txtFileName")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("LanguagePackWriter_txtFileName")));
            }
        }
        public TextField DublinTextField
        {
            get { return ContentPane.TextField(Find.ByValue(s => s.StartsWith("(UTC  00:00)"))); }
        }
        #endregion

        #region Spans
        public Span EditorLanguageLabel
        {
            get { return ContentPane.Span(Find.ById(s => s.EndsWith("languageeditor_languageLabel"))); }
        }
        public Span SystemDefaultLanguageSpan
        {
            get { return ContentPane.Span(Find.ById(s => s.EndsWith("languageEnabler_systemDefaultLanguageLabel"))); }
        }        
        #endregion

        #region Divs
        public Div LanguagesDiv
        {
            get { return IEInstance.Div(Find.ById("dnnLanguages")); }
        }
        public Div DefaultLanguageComboBoxDiv
        {
            get { return IEInstance.Div(Find.ById(new Regex(@"dnn_ctr\d*_languageEnabler_ctl\d*_DropDown"))); }
        }
        public Div EnableLocalizedContentPopUp
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("languageEnabler_toolTipManagerRTMPanel"))); }
        }
        public Div LanguageTranslatorsGrid
        {
            get { return ContentPane.Div(Find.ByClass("dnnRolesGrid")); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox EnableNewLanguageCheckbox
        {
            get { return ContentPane.CheckBox(Find.ById(s => s.EndsWith("EditLanguage_chkEnabled"))); }
        }
        #endregion

        #region FileUploads
        public FileUpload LanguagePackFileUpload
        {
            get { return ContentPane.FileUpload(Find.ById(s => s.EndsWith("WebUpload_cmdBrowse"))); }
        }
        #endregion

        #region RadioButtons
        public RadioButton EditorModeHostRadioButton
        {
            get { return ContentPane.Table(Find.ById(s => s.EndsWith("languageeditor_rbMode"))).RadioButton(Find.ByValue("Host")); }
        }
        #endregion

        #region Images
        public Image NoOfPagesImage
        {
            get { return LanguageTable.Image(Find.ByTitle("No of Pages")); }
        }
        public Image TranslatedPagesImage
        {
            get { return LanguageTable.Image(Find.ByTitle("Translated Pages")); }
        }
        #endregion

        #endregion

        #region Public Methods

        public void AddNewLanguage(string language, string fallbackLanguage)
        {
            AddNewLanguageLink.ClickNoWait();
            System.Threading.Thread.Sleep(1000);
            Element item = null;
            item = SelectLanguage("Language", "rcbItem ", language);
            item.FireEvent("onmouseover");
            item.Click();
            item = SelectLanguage("Fallback", "rcbItem ", fallbackLanguage);
            item.FireEvent("onmouseover");
            item.Click();
            UpdateLink.Click();
        }

        public void ClickExpandButtonForResource(string resourceName)
        {
            DivCollection TreeViewDivs = ContentPane.Divs.Filter(Find.ByClass("rtTop"));
            foreach (Div d in TreeViewDivs)
            {
                if (d.InnerHtml.Contains(resourceName))
                {
                    d.Span(Find.ByClass("rtPlus")).Click();
                    return;
                }
                continue;
            }
        }

        public CheckBox GetCheckBoxForLanguage(string localeName, string type)
        {
            //Returns the checkbox for the Specified Langauge
            CheckBox enabled = null;
            foreach (TableRow row in LanguageTable.OwnTableRows)
            {
                if (row.OwnTableCells.Count == 0)
                {
                    continue;
                }
                if (row.OwnTableCells[0].InnerHtml.Contains(localeName))
                {
                    if (type.CompareTo("Enabled") == 0)
                    {
                        enabled = row.OwnTableCells[1].CheckBox(Find.Any);
                    }
                    else if (type.CompareTo("Published") == 0)
                    {
                        Gallio.Framework.TestLog.WriteLine(row.OwnTableCells.Count + " Table Cells");
                        enabled = row.OwnTableCells[3].CheckBox(Find.ById(s => s.EndsWith("publishedCheckbox")));
                    }
                    break;
                }
                continue;
            }
            return enabled;
        }
       
        public Image GetEditLanguageButtonForLanguage(string localeName)
        {
            //Returns the checkbox for the Specified Langauge
            Image edit = null; ;
            foreach (TableRow row in LanguageTable.OwnTableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(localeName))
                {
                    edit = row.TableCells[2].Image(Find.ByTitle("Edit this language"));
                    break;
                }
                continue;
            }
            return edit;
        }
        
        public Image GetHostLanguageEditButton(string localName)
        {
            //Returns the Edit Button for the language at the host level
            Image result = null;
            foreach (TableRow row in LanguageTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(localName))
                {
                    result = row.Images.Filter(Find.ByTitle("Edit resourcekeys for the host installation"))[0]; ;
                    break;
                }
                continue;
            }
            return result;
        }

        public Span GetLanguageFlagSpanByLocaleName(string localeName)
        {
            return IEInstance.Span(Find.ByTitle(localeName));
        }
       
        public Image GetSiteLanguageEditButton(string localName)
        {
            //Returns the Edit Button for the language at the portal level
            Image result = null;
            foreach (TableRow row in LanguageTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(localName))
                {
                    result = row.Images.Filter(Find.ByTitle("Edit resourcekeys for this portal"))[0];
                    break;
                }
                continue;
            }
            return result;
        }
        
        public Image GetSystemLanguageEditButton(string localName)
        {
            //Returns the Edit Button for the language at the system level
            Image result = null;
            foreach (TableRow row in LanguageTable.TableRows)
            {
                if (row.TableCells[0].InnerHtml.Contains(localName))
                {
                    result = row.Images.Filter(Find.ByTitle("Edit system resourcekeys"))[0]; ;
                    break;
                }
                continue;
            }
            return result;
        }
        
        public void SelectAndUpdateNewDefaultLanguage(string localeName, string selectListClass)
        {
            //Click the drop down Arrow 
            DefaultLanguageDropDownLink.ClickNoWait();

            //Find Item to Select
            //Find all List Item elements.
            ElementCollection SelectListElements = DefaultLanguageComboBoxDiv.Elements.Filter(Find.ByClass(selectListClass));
            Element result = null;
            //Search for the desired Element
            foreach (Element e in SelectListElements)
            {
                if (e.InnerHtml.ToLower().Contains(localeName.ToLower()))
                {
                    //Select the Element
                    result = e;
                    break;
                }
                else
                {
                    continue;
                }
            }
            result.FireEvent("onmouseover");
            System.Threading.Thread.Sleep(1000);
            result.ClickNoWait();
            UpdateLink.Click();
        }
        
        public Element SelectLanguage(string languageType, string SelectListClass, string ItemText)
        {
            Div ComboBox = null;
            //Click Drop down
            if (languageType.Equals("Language"))
            {
                AddLanguageDropDownArrow.Click();
                System.Threading.Thread.Sleep(3000);
                if(PopUpFrame != null)
                {
                    ComboBox = PopUpFrame.Div(Find.ById(s => s.EndsWith("EditLanguage_ctl00_DropDown")));
                }
                else
                {
                    ComboBox = IEInstance.Div(Find.ById(s => s.EndsWith("EditLanguage_ctl00_DropDown")));
                }
                
            }
            else if (languageType.Equals("Fallback"))
            {
                FallbackLanguageDropDownArrow.Click();
                System.Threading.Thread.Sleep(3000);
                if(PopUpFrame != null)
                {
                    ComboBox = PopUpFrame.Div(Find.ById(s => s.EndsWith("EditLanguage_ctl03_DropDown")));
                }
                else
                {
                    ComboBox = IEInstance.Div(Find.ById(s => s.EndsWith("EditLanguage_ctl03_DropDown")));
                }
                
            }
            //Find Item to Select
            //Find all List Item elements.
            ElementCollection SelectListElements = ComboBox.Elements.Filter(Find.ByClass(SelectListClass));
            Element result = null;
            //Search for the desired Element
            foreach (Element e in SelectListElements)
            {
                if (e.InnerHtml.ToLower().Contains(ItemText.ToLower()))
                {
                    //Select the Element
                    result = e;
                    break;
                }
                else
                {
                    continue;
                }
            }
            return result;
        }

        public void SelectLanguagePackType(string packageType)
        {
            if (!string.IsNullOrEmpty(packageType))
            {
                if(PopUpFrame != null)
                {
                    WatiNUtil.SelectRadioButtonByName(PopUpFrame, "LanguagePackWriter$rbPackType", packageType);
                }
                WatiNUtil.SelectRadioButtonByName(IEInstance, "LanguagePackWriter$rbPackType", packageType);
            }
        }

        public void SelectModuleCheckbox(string moduleName)
        {
            foreach (TableCell cell in ModuleCheckboxTable.TableCells)
            {
                if (cell.InnerHtml.Contains(moduleName))
                {
                    cell.CheckBox(Find.Any).Checked = true;
                    break;
                }
                continue;
            }
        }
        #endregion
    }
}
